/**
 * Geneone pager plugin
 *
 * Some of this code is based on the Wordpress TinyMCE plugin
 */
tinyMCE.addToLang('geneone_page',{
	'title' : 'Paging',
	desc : 'Split content into multiple pages.'
});

var TinyMCE_GeneonePagePlugin = {
	getInfo : function() {
		return {
			longname : "Paging",
			author : "Geneone",
			authorurl : "http://geneone.neondragon.net",
			infourl : "http://geneone.neondragon.net",
			version : "1.0"
		};
	},

	/**
	 * Returns the HTML contents of the page control.
	 */
	getControlHTML: function(cn) {
		switch (cn) {
			case "geneone_page":
				return tinyMCE.getButtonHTML(cn, 'lang_geneone_page_desc', '{$pluginurl}/images/page.gif', 'mceGeneonePage');
		}
	
		return "";
	},

	/**
	 * Executes the mceGeneonePage command.
	 */
	execCommand : function(editor_id, element, command, user_interface, value) {
		// Handle commands
		switch (command) {
			case "mceGeneonePage":
				var html = '<p><img src="'+(tinyMCE.getParam("theme_href") + "/images/spacer.gif")+'" width="100%" height="10" class="mce_plugin_geneone_page" /></p>';
				tinyMCE.execCommand("mceInsertContent",true,html);
				tinyMCE.selectedInstance.repaint();
				
				return true;
		}

		// Pass to next handler in chain
		return false;
	},
	
	parseAttributes : function (attribute_string) {
		var attributeName = "";
		var attributeValue = "";
		var withInName;
		var withInValue;
		var attributes = new Array();
		var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');
	
		if (attribute_string == null || attribute_string.length < 2)
			return null;
	
		withInName = withInValue = false;
	
		for (var i=0; i<attribute_string.length; i++) {
			var chr = attribute_string.charAt(i);
	
			if ((chr == '"' || chr == "'") && !withInValue)
				withInValue = true;
			else if ((chr == '"' || chr == "'") && withInValue) {
				withInValue = false;
	
				var pos = attributeName.lastIndexOf(' ');
				if (pos != -1)
					attributeName = attributeName.substring(pos+1);
	
				attributes[attributeName.toLowerCase()] = attributeValue.substring(1).toLowerCase();
	
				attributeName = "";
				attributeValue = "";
			} else if (!whiteSpaceRegExp.test(chr) && !withInName && !withInValue)
				withInName = true;
	
			if (chr == '=' && withInName)
				withInName = false;
	
			if (withInName)
				attributeName += chr;
	
			if (withInValue)
				attributeValue += chr;
		}
	
		return attributes;
	},
	
	cleanup : function(type, content, inst) {
		if (type == "insert_to_editor") {
			var startPos = 0;
			
			// Replace all <!--newpage--> with placer images
			while ((startPos = content.indexOf('<!--newpage-->', startPos)) != -1) {
				// Insert image
				var contentAfter = content.substring(startPos + 14);
				content = content.substring(0, startPos);
				content += '<img src="'+(tinyMCE.getParam("theme_href") + "/images/spacer.gif")+'" width="100%" height="10" class="mce_plugin_geneone_page" />';
				content += contentAfter;

				startPos++;
			}
			
		} else if (type == "get_from_editor") {
			// Parse all img tags and replace them with <!--more-->
			var startPos = -1;
			while ((startPos = content.indexOf('<img', startPos+1)) != -1) {
				var endPos = content.indexOf('/>', startPos);
				var attribs = this.parseAttributes(content.substring(startPos + 4, endPos));

				if (attribs['class'] == "mce_plugin_geneone_page") {
					endPos += 2;
	
					var embedHTML = '<!--newpage-->';
	
					// Insert embed/object chunk
					
					chunkBefore = content.substring(0, startPos);
					chunkAfter = content.substring(endPos);
					content = chunkBefore + embedHTML + chunkAfter;
				}
			}
		}
			
		return content;
	}
};

tinyMCE.addPlugin('geneone_page', TinyMCE_GeneonePagePlugin);
